<?php


namespace app\app\controller;

use app\common\controller\Adminbase;
use think\Controller;
use think\Db;
use app\common\library\DataUtils;

class index2 extends Controller
{
    public function adminOrderList()
    {
        $data = $this->request->param();
        $where[] = ['o.id', '>', 0];
        if (isset($data['id'])&&$data['id']){
            $where[] = ['o.id', '=', $data['id']];
        }
        if (isset($data['shop_name'])&&$data['shop_name']){
            $where[] = ['s.shop_name', 'like', '%'.$data['shop_name'].'%'];
        }

        if (isset($data['phone'])&&$data['phone']){
            $where[] = ['o.phone', '=',  $data['phone']];
        }

        if (isset($data['sale'])&&$data['sale']){
            $where[] = ['sa.name', 'like', '%'.$data['sale'].'%'];
        }

        if (isset($data['kf_status'])&&$data['kf_status']){
            if ($data['kf_status']==3){
                $where[] = ['o.kf_status', '=',  0];
            }else{
                $where[] = ['o.kf_status', '=',  $data['kf_status']];
            }

        }

        if (isset($data['cw_status'])&&$data['cw_status']){
            if ($data['cw_status']==3){
                $where[] = ['o.cw_status', '=',  0];
            }else{
                $where[] = ['o.cw_status', '=',  $data['cw_status']];
            }

        }

        if (isset($data['create_time'])&&$data['create_time']){

            $where[] = ['o.create_time', 'between', [strtotime($data['create_time'][0]), strtotime($data['create_time'][1])]];
        }



        $page = isset($data['page']) && $data['page'] > 0 ? (int)$data['page'] : 1;
        $length = isset($data['limit']) && $data['limit'] > 0 ? (int)$data['limit'] : 10;

        $data_list = Db::name('order')->alias('o')
            ->leftJoin('salesperson sa', 'o.salesperson_id=sa.id')
            ->leftJoin('shop s', 'o.shop_id=s.id')
            ->leftJoin('package p','p.id=o.package_id')
            ->field('o.*,
            sa.name as sa_name,sa.id as sa_id,
            s.id as s_id,s.shop_name,s.people as s_people,s.phone as shop_phone,s.bk_num,s.province,s.city,s.area,
            p.name as package_name2,p.yys
            ')
            ->order('create_time desc')
            ->where($where)
            ->paginate(
                $length, false, [
                    'page' => $page
                ]
            )->each(function ($item) {
                $item['create_time'] = date('Y-m-d H:i', $item['create_time']);

                if ($item['kf_status_time']){
                    $item['kf_status_time'] = date('Y-m-d H:i', $item['kf_status_time']);
                }else{
                    $item['kf_status_time'] = '----';
                }
                if ($item['cw_status_time']){
                    $item['cw_status_time'] = date('Y-m-d H:i', $item['cw_status_time']);
                }else{
                    $item['cw_status_time'] = '----';
                }

                return $item;
            });

        msg(0, '查询成功', $data_list);
    }

    //套餐列表
    public function packList()
    {
        $pack_list = Db::name('package')->where([
            'status' => 1,
            'is_del' => 0
        ])->select();
        msg(0, '查询成功', $pack_list);
    }

    //获取订单详情
    public function orderDetail()
    {
        $data = $this->request->param();
        $data_list = Db::name('order')->alias('o')
            ->leftJoin('salesperson sa', 'o.salesperson_id=sa.id')
            ->leftJoin('shop s', 'o.shop_id=s.id')
            ->field('o.*,sa.name as sa_name,sa.id as sa_id,s.id as s_id,s.shop_name')
            ->where('o.id', '=', $data['id'])
            ->find();
        msg(0, '查询成功', $data_list);
    }

    //客服审核
    public function kfshStatus()
    {
        $data = $this->request->param();
        Db::startTrans();
        try {
            if (!$data['kf_status']) {
                msg(-2, '选择审核状态', '');
            }
            $update_data = array(
                'id' => $data['id'],
                'package_id' => $data['package_id'],
                'sc_money' => $data['sc_money'],

                'hm_img' => $data['hm_img'],
                'rz_img' => $data['rz_img'],
                'cns_img' => $data['cns_img'],

                'kf_status' => $data['kf_status'],
                'kf_status_time' => time(),
                'kf_refuse' => $data['kf_refuse'],
            );

            $pack_info = Db::name('package')->find($data['package_id']);
            //商家特殊返现
            $package_reward1 = Db::name('package_reward')->where([
                'is_del' => 0,
                'type' => 1,
                'package_id' => $data['package_id'],
                'shop_id' => $data['shop_id'],
            ])->find();
            //员工特殊返现
            $package_reward2 = Db::name('package_reward')->where([
                'is_del' => 0,
                'type' => 2,
                'package_id' => $data['package_id'],
                'salesperson_id' => $data['salesperson_id'],
            ])->find();

            if ($data['reward1']) {
                $update_data['reward1'] = $data['reward1'];
                setPackageReward(1, $data['package_id'], $data['shop_id'], $data['reward1']);
            } else {
                if ($package_reward1) {
                    $update_data['reward1'] = $package_reward1['reward1'];
                } else {
                    $update_data['reward1'] = $pack_info['reward1'];
                }
            }
            if ($data['commission1']) {
                $update_data['commission1'] = $data['commission1'];
                setPackageReward(1, $data['package_id'], $data['shop_id'], 0, $data['commission1']);
            } else {
                if ($package_reward1) {
                    $update_data['commission1'] = $package_reward1['commission1'];
                } else {
                    $update_data['commission1'] = $pack_info['commission1'];
                }
            }

            if ($data['reward2']) {
                $update_data['reward2'] = $data['reward2'];
                setPackageReward(2, $data['package_id'], $data['salesperson_id'], $data['reward2']);
            } else {
                if ($package_reward2) {
                    $update_data['reward2'] = $package_reward2['reward2'];
                } else {
                    $update_data['reward2'] = $pack_info['reward2'];
                }
            }

            if ($data['commission2']) {
                $update_data['commission2'] = $data['commission2'];
                setPackageReward(2, $data['package_id'], $data['salesperson_id'], 0, $data['commission2']);
            } else {
                if ($package_reward2) {
                    $update_data['commission2'] = $package_reward2['commission2'];
                } else {
                    $update_data['commission2'] = $pack_info['commission2'];
                }
            }

            $update_data['top_commission2'] = $pack_info['top_commission2'];
            if ($data['kf_status'] == 2) {
                $update_data['status'] = 2;
            }
            Db::name('order')->update($update_data);
            Db::commit();
            msg(0, '审核完成', '');

        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    //财务审核
    public function cwstatus()
    {
        $data = $this->request->param();
        Db::startTrans();
        try {
            if (!$data['cw_status']) {
                $this->error('选择审核状态');
            }
            $update_data = array(
                'id' => $data['id'],
                'cw_status' => $data['cw_status'],
                'status' => $data['cw_status'],
                'cw_refuse' => $data['cw_refuse'],
                'cw_status_time' => time(),
            );
            Db::name('order')->update($update_data);
            if ($data['cw_status'] == 1) {
                $order_info = Db::name('order')->find($data['id']);
                orderSendMoney($order_info);
            }
            Db::commit();
            msg(0, '审核完成', '');
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }
    //异常处理
    public function ycedit(){
        $data = $this->request->param();
        Db::startTrans();
        try {
            $update_data = array(
                'id' => $data['id'],
                'yc' => $data['yc'],
                'money1' => $data['money1'],
                'money2' => $data['money2'],
                'is_z' => $data['is_z'],
                'yc_desc' => $data['yc_desc'],
                'is_yc'=>1,
                'yc_time' => time(),
            );
            Db::name('order')->update($update_data);
            if ($data['money1']){
                //商家扣款
                $shop_info=Db::name('shop')->find($data['shop_id']);
                $member_info=Db::name('member')->find($shop_info['member_id']);
                setMoney($member_info['id'],0,$shop_info['id'],$data['money1'],2,$data['id'],0,0,6);
            }
            if ($data['money2']){
                $salesperson_info=Db::name('salesperson')->find($data['salesperson_id']);
                $member_info=Db::name('member')->find($salesperson_info['member_id']);
                //业务员扣款
                setMoney($member_info['id'],$salesperson_info['id'],0,$data['money2'],2,$data['id'],0,0,5);
            }
            Db::commit();
            msg(0, '设置完成', '');
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }


}